package com.example.online_roll_call.service;

import com.example.online_roll_call.model.Student;
import com.example.online_roll_call.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    // 获取所有学生
    public List<Student> getAllStudents() {
        return studentRepository.findAll();
    }

    // 保存学生
    public Student saveStudent(Student student) {
        return studentRepository.save(student);
    }

    // 标记学生为出勤
    public void markPresent(Long id) {
        // 使用 Optional 处理可能的空值
        Student student = studentRepository.findById(id)
                .orElseThrow(() -> new IllegalArgumentException("学生未找到，ID: " + id));

        student.setPresent(true);
        student.setAttendanceTime(LocalDateTime.now());
        studentRepository.save(student);
    }
    // 根据出勤状态查找学生
    public List<Student> findStudentsByPresent(boolean present) {
        return studentRepository.findByPresent(present);
    }}